//  ========== 
//  = 宾和   用户注册业务JS = 
//  ========== 
var token;
var telphone;
var recommandPerson;
var InterValObj; //timer变量，控制时间  
var count = 59; //间隔函数，1秒执行  
var curCount; //当前剩余秒数  
var Main = function() {
	mui.init({
		swipeBack: false //启用右滑关闭功能
	});
	this.init();
	this.even();
	this.resend();
};
Main.prototype = {
	init: function() {
		telphone = Util.requestURL('telphone');
		recommandPerson = Util.requestURL('recommandPerson');
		$("#telphone").text("+86" + telphone);

		function guid() {
			function s4() {
				return Math.floor((1 + Math.random()) * 0x10000)
					.toString(16)
					.substring(1);
			}
			return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
				s4() + '-' + s4() + s4() + s4();
		}
		token = guid();
	},
	even: function() {
		$(".ipt-real-nick").on("input", function() {
			//console.log($(this).val());
			var $input = $(".ipt-fake-box input");
			if(!$(this).val()) { //无值光标顶置
				$('.ipt-active-nick').css('left', $input.eq(0).offset().left - parseInt($('.ipt-box-nick').parent().css('padding-left')) + 'px');
			}
			if(/^[0-9]*$/g.test($(this).val())) { //有值只能是数字
				//console.log($(this).val());
				var pwd = $(this).val().trim();
				for(var i = 0, len = pwd.length; i < len; i++) {
					$input.eq(i).val(pwd[i]);
					if($input.eq(i).next().length) { //模拟光标，先将图片容器定位，控制left值而已
						$('.ipt-active-nick').css('left', $input.eq(i + 1).offset().left - parseInt($('.ipt-box-nick').parent().css('padding-left')) + 'px');
					}
				}
				$input.each(function() { //将有值的当前input后的所有input清空
					var index = $(this).index();
					if(index >= len) {
						$(this).val("");
					}
				});
				if(len == 6) {
					var code = "";
					$input.each(function() {
						code = code.concat($(this).val());
					});
					//执行其他操作
					console.log('输入完整，执行操作' + code);
					submit(code);
				}
			} else { //清除val中的非数字，返回纯number的value
				var arr = $(this).val().match(/\d/g);
				$(this).val($(this).val().slice(0, $(this).val().lastIndexOf(arr[arr.length - 1]) + 1));
				//console.log($(this).val());
			}
		});

		function submit(code) {
			var password = Util.requestURL('password');
			var registerUrl = server_url + "/user/newsignreg.do";
			var param = {
				isMobile: isMobile,
				custType: "0",
				checkCode: code,
				password: password,
				telphone: telphone
			}
			//如果存在推荐码
			if(recommandPerson) {
				param.recommandPerson = recommandPerson;
			}
			$.getJSON(registerUrl, param, function(retData) {
				if(retData.success == true) {
					mui.openWindow("login.html?telphone=" + telphone);
				} else {
					if(retData.errMsg && retData.errMsg.indexOf("验证码不正确") != -1) {
						mui.toast("请输入正确的验证码");
					} else {
						mui.toast(retData.errMsg);
					}
				}
			});
		}
	},
	resend: function() {
		//timer处理函数  
		function SetRemainTime() {
			if(curCount == 0) {
				window.clearInterval(InterValObj); //停止计时器  
				$("#resendSms").html("<a href='#' id='btn-resendSms'>重新获取</a>");
				$("#btn-resendSms").on('tap', function() {
					//页面层
					var img = server_url + "/getCode.do" + "?token=" + token;
					mui.prompt('<img src="' + img + '"  style="width: 40%;height: 80%; margin-top:10px;" id="btn-sendImg"/>', '请输入验证码', '验证码', ['确定', '取消'], function(btn) {
						if(btn.index > 0) { return; }
						var imgCode = document.querySelector('.mui-popup-input input').value;
						if(!imgCode || imgCode.length != 4) {
							Util.toast('验证码错误');
							return;
						}
						//发送注册短信验证码
						var sendSmsUrl = server_url + "/codecreate.do";
						var param = {
							isMobile: isMobile,
							sms_code_type: 'bind_telphone',
							telphone: telphone,
							randomCode: Math.random(),
							memID: Util.memID(),
							token: token,
							imgCode: imgCode,
							graphicCode: imgCode
						}
						$.getJSON(sendSmsUrl, param, function(retData) {
							if(retData.status == 200) {
								mui.toast("短信验证码发送成功");
								curCount = count;
								$("#resendSms").text(curCount + " 秒后可重发");
								InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次 
							} else {
								mui.toast(retData.errMsg);
							}
						});
					}, 'div');

					function refreshimg() {
						var url = server_url + "/getCode.do";
						$(document.querySelector('.mui-popup-text #btn-sendImg')).attr("src", url + "?token=" + token + "&d=" + Math.random());
					}
					/**刷新图片验证码*/
					$('#btn-sendImg').on('tap', refreshimg);
					document.querySelector('.mui-popup-input input').maxLength = 4;
					$(document.querySelector('.mui-popup-text #btn-sendImg')).on('tap', function() {
						console.log(111)
					});
				});
			} else {
				curCount--;
				$("#resendSms").text(curCount + " 秒后可重发");
			}
		}
		curCount = count;
		$("#resendSms").text(curCount + " 秒后可重发");
		InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次 
	}

};